#include "head.h"
/*  USEAGE: */
void sql_fun(MYSQL *database, sql_t *sql){
    int res = mysql_query(database, sql->query);
    MYSQL_ERROR_CHECK(database, res, 1);
    sql->result = mysql_store_result(database);
    MYSQL_ERROR_CHECK(database, sql->result, NULL);
    sql->row = mysql_fetch_row(sql->result);
    MYSQL_ERROR_CHECK(database,sql->row, NULL);
}


//ls指令
char *fun_ls_by_sql(MYSQL *database, user_info *user, train_t *train){
    //创建sql结构体
    sql_t my_sql;
    char *file_path = user->path;
    int user_id = atoi(user->username);
    printf("now path:%s\n", file_path);
    printf("user_id=%d\n", user_id);
    //根据路径和user_id进行查询获得父id
    
    printf("path:%s\n", file_path);
    sprintf(my_sql.query, 
            "SELECT file_id FROM file_table WHERE path = '%s' AND user_id = '%d'", 
            file_path, user_id);
    sql_fun(database, &my_sql);
    
    int parent_id = atoi(my_sql.row[0]); // 获取 file_id
    printf("fatehr_id:%d\n", parent_id);
    mysql_free_result(my_sql.result); // 释放结果集内存

    //根据父id查询文件名
    memset(my_sql.query, 0, sizeof(my_sql.query));
    sprintf(my_sql.query, "SELECT file_name, flag FROM file_table WHERE father_id = %d", parent_id);
    
    int res = mysql_query(database, my_sql.query);
    MYSQL_ERROR_CHECK(database, res, 1);
    my_sql.result = mysql_store_result(database);
    MYSQL_ERROR_CHECK(database, my_sql.result, NULL);


    int num_rows = mysql_num_rows(my_sql.result);
    printf("nums_rows:%d\n", num_rows);
    int fields = mysql_num_fields(my_sql.result);
    printf("fields:%d\n", fields);
    //拼接
    memset(train->buf, 0, sizeof(train->buf));
    //不能进行清空
    for(int nums = 0; nums < num_rows; nums++){
    
        my_sql.row = mysql_fetch_row(my_sql.result);
        if(my_sql.row[0] == NULL){
            break;
        }
        printf("flag before:%d\n", atoi(my_sql.row[1]));
            if(atoi(my_sql.row[1]) == 0){
                printf("flag after:%d\n", atoi(my_sql.row[1]));
                my_sql.row = mysql_fetch_row(my_sql.result);
                continue;
            }
            char space[2] = "\t"; 
            strcat(train->buf, my_sql.row[0]);
            strcat(train->buf, space);
    }
    mysql_free_result(my_sql.result); // 释放结果集内存
    printf("%s\n", train->buf);
    return train->buf;
}

//mkdir指令
int fun_mkdir_by_sql(MYSQL *conn, user_info *info) {
      // 用于存储SQL查询语句
    int user_id = atoi(info->username);
    char *path =  info->path;
    char *directory_name = info->command_str_goto;
    sql_t my_sql;
    memset(&my_sql, 0, sizeof(my_sql));
    sprintf(my_sql.query,
            "select file_id from file_table where user_id = %d and path ='%s'",
            user_id, path
            );
    sql_fun(conn, &my_sql);

    int father_id = atoi(my_sql.row[0]);
    mysql_free_result(my_sql.result);

    memset(&my_sql, 0, sizeof(my_sql));
    // 构造检查是否存在相同文件名的SQL查询语句
    snprintf(my_sql.query, sizeof(my_sql.query),
             "SELECT file_id,flag  FROM file_table WHERE user_id = %d AND path = '%s/%s' AND file_name = '%s'",
             user_id, path, directory_name, directory_name);

    int res = mysql_query(conn, my_sql.query);
    MYSQL_ERROR_CHECK(conn, res, 1);
    my_sql.result = mysql_store_result(conn);
    MYSQL_ERROR_CHECK(conn, my_sql.result, NULL);
    my_sql.row = mysql_fetch_row(my_sql.result);
    int num_row = mysql_num_rows(my_sql.result);
    memset(my_sql.query, 0, sizeof(my_sql.query));
    // 存储查询结果
    if (num_row != 0){
        if(atoi(my_sql.row[1]) == 0){
            printf("存在文件且flag = 0");
            printf("flag:%d\n", atoi(my_sql.row[1]));
            sprintf(my_sql.query,
           " UPDATE file_table  SET flag  = 1  WHERE user_id = %d AND path = '%s/%s' AND file_name = '%s'",
           user_id, path, directory_name, directory_name
           );
            res = mysql_query(conn, my_sql.query);
            MYSQL_ERROR_CHECK(conn, res, 1);
            return 1;
        }
        else
        return 0;
    }
    else if(num_row == 0)
    {
        memset(&my_sql, 0, sizeof(my_sql));
        snprintf(my_sql.query, sizeof(my_sql.query),
                 "INSERT INTO file_table (file_properties, father_id, flag, path, file_name, user_id) "
                 "VALUES (1, %d, 1, '%s/%s', '%s', %d)",
                 father_id, path, directory_name, directory_name, user_id);
        res = mysql_query(conn, my_sql.query);
        MYSQL_ERROR_CHECK(conn, res, 1);
    }
        mysql_free_result(my_sql.result);
        return 1;
}

int fun_rm_by_sql(MYSQL *conn, user_info *info){
    printf("rm\n");
    sql_t my_sql;
    memset(&my_sql, 0, sizeof(my_sql));
    sprintf(my_sql.query,
            "select file_id from file_table where user_id = %d AND path ='%s/%s' AND flag = 1",
            atoi(info->username), info->path,info->command_str_goto);
    int res =mysql_query(conn, my_sql.query);
    MYSQL_ERROR_CHECK(conn, res, 1);
    my_sql.result = mysql_store_result(conn);
    MYSQL_ERROR_CHECK(conn, my_sql.result, NULL);
    my_sql.row = mysql_fetch_row(my_sql.result);
    
    //不存在该数据，删除失败
    if(my_sql.row == NULL){
        return 0;
    }
    mysql_free_result(my_sql.result);
    
    memset(&my_sql, 0, sizeof(my_sql));
    sprintf(my_sql.query,
            "UPDATE file_table  SET flag  = 0 WHERE user_id = %d AND path LIKE '%s/%s%%'",
            atoi(info->username), info->path, info->command_str_goto);
    res =mysql_query(conn, my_sql.query);
    MYSQL_ERROR_CHECK(conn, res, 1);
    return 1;
}
